<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="课程：" name="courseId">
						<a-select
							v-model:value="searchFormState.courseId"
							show-search
							:options="courseData"
							:filter-option="filterOption"
							:field-names="{ label: 'name', value: 'id' }"
							style="width: 100%"
							placeholder="请选择课程"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="教师：" name="teacherId">
						<a-select
							v-model:value="searchFormState.teacherId"
							show-search
							:options="teacherData"
							:filter-option="filterOption"
							:field-names="{ label: 'name', value: 'id' }"
							style="width: 100%"
							placeholder="请选择教师"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="班级：" name="klassId">
						<a-select
							v-model:value="searchFormState.klassId"
							show-search
							:options="klassData"
							:filter-option="filterOption"
							:field-names="{ label: 'name', value: 'id' }"
							style="width: 100%"
							placeholder="请选择班级"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="状态" name="state">
						<a-select
							v-model:value="searchFormState.state"
							:options="stateOptions"
							style="width: 100%"
							placeholder="请选择"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('eduLectureAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<a-button @click="createLesson()">
						<template #icon><export-outlined /></template>
						批量排课
					</a-button>
					<a-button @click="calcClassHour()">
						<template #icon><export-outlined /></template>
						统计课时
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('eduLectureBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchEduLecture"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'roomType'">
					{{ $TOOL.dictTypeData('ROOM_TYPE', record.roomType) }}
				</template>
				<template v-if="column.dataIndex === 'state'">
					{{ $TOOL.dictTypeData('LECTURE_STATE', record.state) }}
				</template>
				<template v-if="column.dataIndex === 'fromWeekToWeek'">
					{{ record.fromWeek + "-" + record.toWeek }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('eduLectureEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['eduLectureEdit', 'eduLectureDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteEduLecture(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('eduLectureDelete')">删除</a-button>
						</a-popconfirm>
						<a-divider type="vertical" v-if="hasPerm(['eduLectureEdit', 'eduLectureDelete'], 'and')" />
						<a @click="lessonRef.onOpen(null, record.id)" v-if="hasPerm('eduLectureEdit')">排课</a>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
	<Lesson ref="lessonRef" @successful="table.refresh(true)" />
	<Module ref="moduleRef" @successful="table.refresh(true)" />
</template>

<script setup name="lecture">
	import router from '@/router'
	import Form from './form.vue'
	import Lesson from '../lesson/form.vue'
	import eduKlassApi from '@/api/biz/eduKlassApi'
	import eduLectureApi from '@/api/biz/eduLectureApi'
	import eduLessonApi from "@/api/biz/eduLessonApi";
	import { message } from 'ant-design-vue'
	import tool from '@/utils/tool'
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const lessonRef = ref()
	const moduleRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)

	// 教师数据
	let teacherData = ref([])
	// 班级数据
	let klassData = ref([])
	//课程数据
	let courseData = ref([])
	// 状态数据
	const stateOptions = tool.dictList('LECTURE_STATE')

	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '课程名称',
			dataIndex: 'courseName'
		},
		{
			title: '教师',
			dataIndex: 'teacherName'
		},
		{
			title: '班级',
			dataIndex: 'klassName'
		},
		{
			title: '人数',
			dataIndex: 'size'
		},
		{
			title: '起止周',
			dataIndex: 'fromWeekToWeek'
		},
		{
			title: '状态',
			dataIndex: 'state'
		},
		{
			title: '教室类型',
			dataIndex: 'roomType'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['eduLectureEdit', 'eduLectureDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '230px'
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
    const filterOption = (input, option) => {
		return option.name.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    }
	const loadData = (parameter) => {
		seleKlassData()
		seleTeacherData()
		seleCourseData()
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return eduLectureApi.eduLecturePage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}

	// 选择班级
	const seleKlassData = () => {
		const param = {}
		eduKlassApi.eduKlassList(param).then((res) => {
			klassData.value = res
		})
	}
	// 选择教师
	const seleTeacherData = () => {
		const param = {}
		eduLectureApi.eduTeacherList(param).then((res) => {
			teacherData.value = res
		})
	}
	// 选择课程
	const seleCourseData = () => {
		const param = {}
		eduLectureApi.eduCourseList(param).then((res) => {
			courseData.value = res
		})
	}

	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 删除
	const deleteEduLecture = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		eduLectureApi.eduLectureDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 一键排课
	const createLesson = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择要排课的记录')
			return
		}
		let params = {
			lectureIds: selectedRowKeys.value
				.map((m) => {
					return m
				})
				.join()
		}
		eduLessonApi.eduLessonCreate(params).then(() => {
			message.success('操作成功')
			table.value.clearSelected()
			table.value.refresh(true)
		})
	}
	// 统计课时
	const calcClassHour = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择要统计的记录')
			return
		}
		let params = {
			lectureIds: selectedRowKeys.value
				.map((m) => {
					return m
				})
				.join()
		}
		calculate(params)
		return
	}
	const calculate = (params) => {
		eduLectureApi.eduLectureCalculate(params).then(() => {
			message.success('操作成功')
			table.value.clearSelected()
		})
	}
	// 批量删除
	const deleteBatchEduLecture = (params) => {
		eduLectureApi.eduLectureDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
	const leaveFor = (url = '/', lectureId) => {
		router.replace({
			path: url,
			params: { lectureId: lectureId }
		})
	}
</script>
